//
//  ScrollViewController.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/7.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit

class ScrollViewController: UIViewController {

    lazy var itemSize = CGSize(width: self.view.frame.width/4*3, height: 180)
    lazy var itemSize1 = CGSize(width: self.view.frame.width/3*2, height: 300)

    lazy var collectionView: UICollectionView = {
        let layout = ScrollLayout.init()
        layout.scrollDirection = .horizontal
        layout.itemSize = itemSize
        layout.minimumInteritemSpacing = 0.0001
        layout.minimumLineSpacing = -40
        
        let cv = UICollectionView.init(frame: CGRect(x: 0, y: 100, width: self.view.frame.width, height: 200), collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        cv.register(UINib.init(nibName: "ScrollCell", bundle: nil), forCellWithReuseIdentifier: "ScrollCellID")
        
        return cv
    }()
    
    lazy var collectionView1: UICollectionView = {
        let layout = ScrollLayout.init()
        layout.scrollDirection = .vertical
        layout.itemSize = itemSize1
        layout.minimumInteritemSpacing = 0.0001
        layout.minimumLineSpacing = -40
        
        let cv = UICollectionView.init(frame: CGRect(x: 0, y: 350, width: self.view.frame.width, height: 450), collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        cv.register(UINib.init(nibName: "ScrollCell", bundle: nil), forCellWithReuseIdentifier: "ScrollCellID1")
        
        return cv
    }()

    
    let colors: [UIColor] = [UIColor.init(named: "Grey")!, UIColor.init(named: "LightOrange")!, UIColor.init(named: "LightRed")!, UIColor.init(named: "Primary")!]
    let images: [String] = ["3", "4", "5", "6", "7", "8", "9"]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.safeAreaInsetsDidChange()
        view.backgroundColor = UIColor.white
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        view.addSubview(collectionView)
        view.addSubview(collectionView1)
    }
}

extension ScrollViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.collectionView {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScrollCellID", for: indexPath) as! ScrollCell
            cell.imgView.backgroundColor = colors[indexPath.row%4]
            cell.imgView.image = UIImage.init(named: images[indexPath.row%4])
            return cell
        }
        else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScrollCellID1", for: indexPath) as! ScrollCell
            cell.imgView.image = UIImage.init(named: images[indexPath.row%4])
            return cell
        }

    }
}
